/**
 * Created by wushanxin on 14-4-8.
 */

var log = require("../lib/log");
var config = require('../config');
var crypto = require('crypto');
var RUtil = require('./dataformatUtil');


var db = require("../lib/db");
var viruser=require('../models/viruser.js');
exports.index = function(request, response) {

    var method = request.method || '';

    if(typeof(request.session.admin)=="undefined" || request.session.admin==null || request.session.admin.length==0)
    {
        response.send("<script>window.parent.location.href='login.html';</script>");
        return;
    }

    if(request.session.roleinfo.flag==0)
    {
        response.send("<script>window.parent.location.href='index.html';</script>");
        return;
    }
    var viruserdata=RUtil.viruserdata(request);
    viruserdata.retdata.nickname =request.session.admin.nickname;
    viruserdata.retdata.roleinfo = request.session.roleinfo;
    viruserdata.retdata.userinfo = request.session.admin;
    viruserdata.retdata.title="权限管理";
    if(method.toUpperCase() === "GET") {
        if(request.session.roleinfo.flag==1)
        {
            viruser.selectByRoleType(request.session.admin.id, function(error,results) {
                if((error)!== null)
                {
                    // 数据库异常
                    viruserdata.retdata.info = "载入失败：系统内部错误(错误码："+error.errno+")";
                    viruserdata.retdata.success=false;
                    viruserdata.virusers= null;
                    response.render("roleadmin", viruserdata);
                }
                else if(results.length <= 0) {
                    // 不存在，直接响应页面
                    viruserdata.retdata.info = "没有任何人员记录!";
                    viruserdata.retdata.success=false;
                    viruserdata.roles=null;
                    viruserdata.virusers= null;
                    response.render("useradmin", viruserdata);
                } else {
                    //存在数据将数据发到页面
                    viruserdata.retdata.info = "人员记录提取成功!";
                    viruserdata.retdata.success=true;
                    for(var i = 0; i < results.length; i++)
                    {
                        results[i].authnumber=RUtil.getspecialauthlist(results[i].authnumber);
                        results[i].statnumber=RUtil.getspecialstatlist(results[i].statnumber);
                    }
                    viruserdata.virusers= results;

                    response.render("useradmin", viruserdata);
                }
            });
        }
        if(request.session.roleinfo.flag==99)
        {
            viruser.selectAll( function(error,results) {
                if((error)!== null)
                {
                    // 数据库异常
                    viruserdata.retdata.info = "载入失败：系统内部错误(错误码："+error.errno+")";
                    viruserdata.retdata.success=false;
                    response.render("roleadmin", viruserdata);

                }
                else if(results.length <= 0) {
                    // 不存在，直接响应页面
                    viruserdata.retdata.info = "没有任何人员记录!";
                    viruserdata.retdata.success=false;
                    viruserdata.virusers=null;
                    response.render("useradmin", viruserdata);
                } else {
                    //存在数据将数据发到页面
                    viruserdata.retdata.info = "人员记录提取成功!";
                    viruserdata.retdata.success=true;
                    for(var i = 0; i < results.length; i++)
                    {
                        results[i].authnumber=RUtil.getspecialauthlist(results[i].authnumber);
                        results[i].statnumber=RUtil.getspecialstatlist(results[i].statnumber);
                    }
                    viruserdata.virusers= results;

                    response.render("useradmin", viruserdata);
                }
            });
        }


    }

    else if(method.toUpperCase() === "POST") {
        if(request.body.oper == "del")
        {
            var delid = request.body.id;
            viruser.deleteByID(delid, function(error,results) {
                if((error)!== null)
                {
                    // 数据库异常
                    var info = { status:"success",info:'删除失败：系统内部错误(错误码：'+error.errno+')' };
                    response.send((info));
                }
                else if(results.length <= 0) {
                    // 不存在，直接响应页面
                    var info = { status:"error",info:'删除失败！' };
                    response.send( (info));
                } else {
                    //存在数据将数据发到页面
                    var info = { status:"success",info:'删除成功！' };
                    response.send( (info));
                }
            });
        }
    }

};


